home *** CD-ROM | disk | FTP | other *** search
- /* ========== the commmand file: ==========
-
- man.c - where normal Macintosh stuff is handled.
-
- Copyright (c) 1993,1994 Newport Software Development
-
- You may distribute unmodified copies of this file for
- noncommercial purposes. You may use this file as a
- reference when writing your own nShell(tm) commands.
-
- All other rights are reserved.
-
- ========== the commmand file: ========== */
-
- #include "nshc.h"
-
- #include "str_utl.proto.h"
- #include "nshc_utl.proto.h"
-
- extern short ResErr;
-
- /* ========== Utility Functions. ========== */
-
- void man_display( t_nshc_calls *nshc_calls, char **text );
-
- void man_display( t_nshc_calls *nshc_calls, char **text )
- {
- long i;
- char *p;
-
- i = SizeResource(text);
- SetHandleSize(text,i+2);
- HLock(text);
- p=*text;
- if ( p[i-1] != '\r' ) {
- p[i]='\r';
- p[i+1]=0;
- }
- else
- p[i]=0;
- nshc_calls->NSH_puts(p);
- HUnlock(text);
- ReleaseResource(text);
- }
-
- /* ========== find the resource in the current path ========== */
- /*
- Construct a name in the format "man parm1 parm2", where parm1
- and parm2 are the parameters given to the "man" command. If no
- second parameter is given, default to "man parm1 general".
- */
-
- int man_in_memory(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls);
-
- int man_in_memory(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
- {
- char **text;
- Str255 rsrcName;
-
- pStrFromC( rsrcName, "man ");
-
- pStrAppendC( rsrcName, &nshc_parms->arg_buf[nshc_parms->argv[1]]);
- pStrAppendC( rsrcName, " " );
-
- if (nshc_parms->argc > 2)
- pStrAppendC( rsrcName, &nshc_parms->arg_buf[nshc_parms->argv[2]] );
- else
- pStrAppendC( rsrcName, "general");
-
- text = (char **)GetNamedResource( 'TEXT', rsrcName);
-
- if (text) {
- man_display( nshc_calls, text );
- return(1);
- }
- else
- return(0);
- }
-
- /* ========== find the resource in the specified command ========== */
-
- int man_in_file(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls);
-
- int man_in_file(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
- {
- char **text;
- Str255 fileName;
- Str255 rsrcName;
- short iFileRef;
-
- pStrFromC( fileName, &nshc_parms->arg_buf[nshc_parms->argv[1]] );
-
- iFileRef = -1;
-
- if ( !nshc_calls->NSH_path_which( fileName ) )
- iFileRef = OpenResFile( fileName );
-
- if ( iFileRef < 0 ) {
- nshc_calls->NSH_putStr_err("\pman: Command file not found.\r");
- return(0);
- }
-
- pStrFromC( rsrcName, "man ");
-
- if (nshc_parms->argc > 2)
- pStrAppendC( rsrcName, &nshc_parms->arg_buf[nshc_parms->argv[2]] );
- else
- pStrAppendC( rsrcName, "general");
-
- text = (char **)GetNamedResource( 'TEXT', rsrcName);
-
- if ( !text )
- nshc_calls->NSH_putStr_err("\pman: Manual Page Resource Not Found.\r");
- else
- man_display( nshc_calls, text );
-
- if (iFileRef)
- CloseResFile(iFileRef);
-
- return(text != 0);
- }
-
- /* ========== the command. ========== */
-
- void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
- {
- int result;
- int argc;
-
- nshc_parms->action = nsh_idle; // assume completion
- result = 1; // assume failure (resource not found)
-
- if (nshc_bad_version( nshc_parms, nshc_calls, NSHC_VERSION )) return;
-
- argc = nshc_parms->argc;
-
- if ((argc <2) || (argc >3)) {
- nshc_calls->NSH_putStr_err("\pUsage: man command [section].\r");
- result = NSHC_ERR_PARMS;
- }
- else
- if ( man_in_memory( nshc_parms, nshc_calls ) )
- result = NSHC_NO_ERR;
- else
- if ( man_in_file( nshc_parms, nshc_calls ) )
- result = NSHC_NO_ERR;
-
- nshc_parms->result = result;
- nshc_parms->action = nsh_idle;
-
- ResErr = 0; // reset the global resource error after our searches
- }
-